package com.dlxj.service.impl;

import java.util.ArrayList;
import java.util.Currency;
import java.util.List;

import com.dlxj.dao.BaseDAO;
import com.dlxj.entity.Circuitry;
import com.dlxj.entity.CircuitryPole;
import com.dlxj.entity.Pole;
import com.dlxj.service.CircuitryService;
import com.dlxj.util.ConstClass;
import com.dlxj.util.PageBean;

public class CircuitryServiceImpl implements CircuitryService {

	BaseDAO baseDAO;

	@Override
	public List<Circuitry> findCircuitryList() {
		return (List<Circuitry>) baseDAO.findByHql("from Circuitry");
	}

	@Override
	public List<Pole> findPoliesByCid(String cid) {

		List<Pole> polies = new ArrayList<Pole>();
		
		
				
				// 获取线路上的所有塔杆
				List<CircuitryPole> cps = (List<CircuitryPole>) baseDAO.findByHql(
						"from CircuitryPole where cid= ? order by position asc",
						new Object[] { cid });

				
				
				// 获取线路名称
				String routeName = ((Circuitry) baseDAO.findById(Circuitry.class, cid)).getName();

				
				// 为塔杆设置线路名称
				for (CircuitryPole circuitryPole : cps) {
					Pole pole = (Pole) baseDAO.findById(Pole.class,
							circuitryPole.getPid());
					pole.setRouteName(routeName);
					polies.add(pole); // 添加到集合中
				}
		
		return polies;
	}

	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	@Override
	public int addCircuitry(Circuitry circuitry,
			List<CircuitryPole> circuitryPolies) {

		circuitry.setTowercount(circuitryPolies.size());// 设置塔的数量
		circuitry.setArroundlength(circuitry.getLength());// 设置回路长度
		circuitry.setState(ConstClass.ENABLE);// 状态
		int count = baseDAO.countHql(
				"select count(*) from Circuitry where name=? or id=?",
				new Object[] { circuitry.getName(), circuitry.getId() });// 验证
		if (count > 0) {
			return ConstClass.ADD_FAILURE;
		}
		baseDAO.save(circuitry);
		for (CircuitryPole circuitryPole : circuitryPolies) {
			circuitryPole.setCid(circuitry.getId());
			baseDAO.save(circuitryPole); // 添加杆塔
		}
		return ConstClass.ADD_SUCCESS;
	}

	@Override
	public List<List<Pole>> findAllPolies() {
		List<Circuitry> circuitries = this.findCircuitryList();// 获取所有路线
		List<List<Pole>> result = new ArrayList<List<Pole>>();
		for (Circuitry circuitry : circuitries) {
			List<Pole> polies = this.findPoliesByCid(circuitry.getId());// 根据路线获取杆塔
			result.add(polies);
		}
		return result;
	}

	@Override
	public List<Circuitry> findCircuitriesByPid(String pid) {
		List<CircuitryPole> cps = (List<CircuitryPole>) baseDAO.findByHql(
				"from CircuitryPole where pid = ?", new Object[] { pid });
		List<Circuitry> circuitries = new ArrayList<Circuitry>();
		for (CircuitryPole circuitryPole : cps) {
			Circuitry circuitry = (Circuitry) baseDAO.findById(Circuitry.class,
					circuitryPole.getCid());
			circuitries.add(circuitry);
		}
		return circuitries;
	}

	@Override
	public int deleteRoutLine(String id) {
		String cid = id;
		baseDAO.delete(Circuitry.class, id);
		baseDAO.listDelete(cid);
		return ConstClass.DELETE_SUCCESS;
	}

	@Override
	public Circuitry findStartPoleByCName(String name) {
		return (Circuitry) baseDAO.findByHql(
				"from Circuitry where name = ?", new Object[] { name });
	}

	@Override
	public List<Circuitry> findcircuitry(Circuitry circuitry) {
		List<Circuitry> circuitryList=new ArrayList<Circuitry>();
		try {
			String hql="FROM Circuitry WHERE 1=1 ";
			String wherestr = "";
			List<Object> args=new ArrayList<Object>();
			if(!(circuitry.getName().equals("0"))){
				wherestr=" and name=?";
				args.add(circuitry.getName());
			}
			/*System.out.println(circuitry.getName());
			System.out.println(circuitry.getPoleByStartpoleNo().getId());
			System.out.println(circuitry.getPoleByEndpoleNo().getId());
			*/
			
			if(!circuitry.getPoleByStartpoleNo().getId().equals("0")&&!circuitry.getPoleByEndpoleNo().getId().equals("0")
					&&!circuitry.getPoleByStartpoleNo().getId().equals("")
					&&!circuitry.getPoleByEndpoleNo().getId().equals("")
					){
				wherestr+=" and (startpoleNo=? OR endpoleNo=?)";
				args.add(circuitry.getPoleByStartpoleNo().getId());
				args.add(circuitry.getPoleByEndpoleNo().getId());
			}
			hql+=wherestr;
			Object[] params=args.toArray();
			if(wherestr.equals("")){
				circuitryList=this.baseDAO.findByHql(hql);
			}else{
				circuitryList=this.baseDAO.findByHql(hql,params);
			}
			
			System.out.println(circuitryList.size());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return circuitryList;
	}

	@Override
	public List<Pole> findPoleByStartpoleNo(String name) {
		
		/*Circuitry cts=(Circuitry) baseDAO.findByHql(
				"from Circuitry where name = ?", new Object[] { name });
		
		List<Pole> pole = (List<Pole>) baseDAO.findByHql(
				"FROM Pole where id = ?", new Object[] { cts.getPoleByStartpoleNo().getId() });*/
		name="'"+name+"'";
		String sql="select * from pole where id = (select startpoleNo from circuitry where name="+name+")";
		List<Pole>poles=baseDAO.findBySql(sql);
		
			return poles;
	}

	@Override
	public PageBean findcircuitrybyPage(Circuitry circuitry, int pageIndex,
			int pageSize) {
		PageBean bean=new PageBean();
		try {
			String hql="from Circuitry WHERE 1=1 ";
			String wherestr = "";
			List<Object> args=new ArrayList<Object>();
			if(!(circuitry.getName().equals("0"))){
				wherestr=" and name=?";
				args.add(circuitry.getName());
			}
			/*System.out.println(circuitry.getName());
			System.out.println(circuitry.getPoleByStartpoleNo().getId());
			System.out.println(circuitry.getPoleByEndpoleNo().getId());
			*/
			
			if(!circuitry.getPoleByStartpoleNo().getId().equals("0")&&!circuitry.getPoleByEndpoleNo().getId().equals("0")
					&&!circuitry.getPoleByStartpoleNo().getId().equals("")
					&&!circuitry.getPoleByEndpoleNo().getId().equals("")
					){
				wherestr+=" and (startpoleNo=? OR endpoleNo=?)";
				args.add(circuitry.getPoleByStartpoleNo().getId());
				args.add(circuitry.getPoleByEndpoleNo().getId());
			}
			hql+=wherestr;
			Object[] params=args.toArray();
			if(wherestr.equals("")){
				bean= baseDAO.findPageBeanHQL(hql, pageIndex, pageSize,
						new Object[]{}); 
			}else{
				bean= baseDAO.findPageBeanHQL(hql, pageIndex, pageSize,
						params); 
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bean;
	}
	

}
